package com.frxuexi.park2304

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvi.BaseViewActivity
import com.frxuexi.lib_base.utils.LiveDateBus
import com.frxuexi.park2304.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = "/app/MainActivity")
@AndroidEntryPoint
class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    override val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    /*override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ARouter.getInstance().build("/shop/ShopActivity").navigation()


        lifecycleScope.launch {
            (1..10).asFlow().map {
                it*3
            }.filter {
                it%2==0
            }.buffer().collect{
                Log.i("lr",it.toString())
            }
        }

    }*/

    override fun initView() {
        EventBus.getDefault().register(this)
        val navController = findNavController(R.id.fragment_main)
        NavigationUI.setupWithNavController(binding.nav,navController)

        LiveDateBus.with("nav",String::class.java).observe(this){
            if(it=="3"){
                binding.nav.selectedItemId=R.id.cartFragment
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getMsg(msg:String){
        if(msg=="3"){
            ToastUtils.showLong(msg)
        }
    }
}